package com.jrx.anytxn.transaction.timer.job.payment.config;

import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.timer.job.payment.step.PaymentProcessor;
import com.jrx.anytxn.transaction.timer.job.payment.step.PaymentWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 还款任务
 *
 * @author jing.xy
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class PaymentConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private PaymentWriter paymentWriter;

    @Autowired
    private PaymentProcessor paymentProcessor;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    public GrayVersionConfig grayVersionConfig;

    @Bean
    public Job paymentJob() {
        return jobBuilderFactory.get("paymentJob")
                .start(paymentStep())
                .build();
    }

    @Bean
    public Step paymentStep() {
        return stepBuilderFactory.get("paymentStep")
                .<TlPaymentLog,TlPaymentLog>chunk(anytxnBatchProperties.getPaymentConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(paymentReader())
                .processor(paymentProcessor)
                .writer(paymentWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getPaymentConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlPaymentLog> paymentReader() {
        //同步reader   线程安全
        MyBatisCursorItemReader<TlPaymentLog> accrualCursorReader = new MyBatisCursorItemReader<>();
        accrualCursorReader.setSaveState(false);
        accrualCursorReader.setSqlSessionFactory(sqlSessionFactory);
        accrualCursorReader.setQueryId("com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentLogMapper.selectBySumStatusNotPayType");
        Map<String, Object> map = new HashMap<>();
        map.put("sumStatus", TransBizConstant.PAYMENT_LOG_SUM_STATUS_D);
        map.put("payTypeList", Arrays.asList(TransBizConstant.REPAYMENT_TYPE_AUTO,TransBizConstant.REPAYMENT_TYPE_CMPS,TransBizConstant.REPAYMENT_TYPE_CRT,TransBizConstant.REPAYMENT_TYPE_CHARGEOFF));
        map.put("grayVersion", grayVersionConfig.getCurrentVersion());
        accrualCursorReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<TlPaymentLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(accrualCursorReader);
        return synchronizedItemStreamReader;
    }


}
